package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;
import org.json.JSONObject;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.regex.Pattern;
import butterknife.BindView;
import butterknife.OnClick;
/**
 * 用于蓝箭点数
 */
public class BreLanjianPointNumMainActivity extends BaseActivity {

    private LotInfoEntity lotInfoEntity;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.xinghao)
    FreeUI_ClearEditText xinghao;
    @BindView(R.id.fengzuang)
    EditText fengzuang;
    @BindView(R.id.shuliang)
    EditText shuliang;
    @BindView(R.id.tvPointType)
    TextView tvPointType;
    @BindView(R.id.ivPointArrow)
    ImageView ivPointArrow;
    @BindView(R.id.flPointList)
    FrameLayout flPointList;
    @BindView(R.id.true_num)
    EditText true_shuliang;
    @BindView(R.id.shebei)
    EditText shebei;
    @BindView(R.id.btn_pass)
    RadioButton btn_pass;
    @BindView(R.id.btn_ng)
    RadioButton btn_ng;
    @BindView(R.id.beizhu)
    EditText beizhu;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;

    // 日期，工单，批次，数量，实际数量，客户，封装形式，型号，公步描述，公步，账号，名字，点数设备，设备，是否PASS,备注
    private String SEND_TIME;
    private String WO_ID;
    private int MAIN_QTY=0;
    private int TRUE_MAIN_QTY=0;  // 略
    private String CUSTOMER_CODE;
    private String STEP_DESC;
    private String STEP_NAME;
    private String USER_NAME;  // 略
    private String NAME;  // 略
//    private String LOCATION;
    private String IS_PASS;   // 略
    private String BEI_ZU;   // 略

    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private ArrayList<String> city = new ArrayList<>(Arrays.asList("JB-0001","JB-0002","JB-0003","JB-0010"));

    private FreeUI_GeneralPop codePop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bre_lanjian_point_num_main;
    }

    @Override
    protected void initView() {

        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);

         initEdit(etLotID, sa -> {
        //获取lot信息
        LotUtils.searchLotInfo(getApplicationContext(), sa, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity body) {
                lotInfoEntity = body;
                if(body.getLOT() != null || !body.getLOT().equals("null") ){
                    fengzuang.setText(body.getLOT().getMAINMATTYPE());
                    shuliang.setText(body.getLOT().getMAINQTY());
                    if(body.getLOT().getPROCEDURENAME() != null){
                        xinghao.setText(body.getLOT().getUDF().getProductModel());
                    }else{
                        Toast.makeText(getApplicationContext(), "该批次型号为空", Toast.LENGTH_SHORT).show();
                    }
                    if(body.getLOT().getLOCATION() != null){
                        shebei.setText(body.getLOT().getLOCATION());
                    }else{
                        Toast.makeText(getApplicationContext(), "该批次设备为空", Toast.LENGTH_SHORT).show();
                    }
//                                扫描带出的数据
                    WO_ID=body.getLOT().getWOID();
                    MAIN_QTY=Integer.parseInt(body.getLOT().getMAINQTY());
                    CUSTOMER_CODE=body.getLOT().getCUSTOMERCODE();
                    STEP_DESC=body.getLOT().getSTEPDESC();
                    STEP_NAME=body.getLOT().getSTEPNAME();
//                                LOCATION=body.getLOT().getLOCATION();
                }
            }
            @Override
            public void onFail(Throwable t) {

            }
        });
    });

        flPointList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPointList.getWidth(), city, ivPointArrow, (ty, i) -> {
                        tvPointType.setText(ty);
//                        POIN_LOCATION=ty;
                        Toast.makeText(getApplicationContext(),ty,Toast.LENGTH_SHORT).show();
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivPointArrow);
                codePop.showAsDropDown(flPointList, 0, 0);
            }
        });
    }

    @Override
    protected void initData() {
        etLotID.requestFocus();
    }

    @OnClick({ R.id.tvBaseSubmit,R.id.btn_pass,R.id.btn_ng})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                submit();
                break;
            case R.id.btn_pass:
                Toast.makeText(getApplicationContext(),"btn_pass",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_ng:
                Toast.makeText(getApplicationContext(),"btn_ng",Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private void btnClean1(){
        etLotID.setText(null);
        xinghao.setText(null);
        fengzuang.setText(null);
        shuliang.setText(null);
        true_shuliang.setText(null);
        shebei.setText(null);
        beizhu.setText(null);
        btn_ng.setChecked(false);
        btn_pass.setChecked(false);
        tvPointType.setText(null);
    }
    private void submit(){
        SEND_TIME = format.format(new Date());
        USER_NAME=Constant.CURRENT_USER;
        NAME=Constant.CURRENT_DESC;
        if(getText(etLotID).equals("")){
            showToast("请先扫描批次信息", false);
            return;
        }
        if(getText(true_shuliang).equals("")){
            showToast("请输入批次实际数量", false);
            return;
        }
        Pattern pattern = Pattern.compile("^[-]?[\\d]+$");
        boolean isNum = pattern.matcher(getText(true_shuliang).trim()).matches();
        if(!isNum){
            showToast("批次实际数量需为整数", false);
            return;
        }
        if(tvPointType.getText().toString().equals("") || tvPointType.getText().toString()==null){
            showToast("请选择点数设备!!", false);
            return;
        }
        if(!btn_pass.isChecked() && !btn_ng.isChecked()){
            showToast("请选择PASS或者NG!!", false);
            return;
        }
        TRUE_MAIN_QTY=Integer.parseInt(true_shuliang.getText().toString().trim());
        BEI_ZU=beizhu.getText().toString().trim();
        IS_PASS=btn_pass.isChecked()==true?"PASS":"NG";
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("成功", true);
                    btnClean1();
                    etLotID.requestFocus();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "PDA_POINT_NUMBER");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("WO_ID", WO_ID);
        map.addProperty("LOT_ID", getText(etLotID));
        map.addProperty("MAIN_QTY", MAIN_QTY);
        map.addProperty("TRUE_MAIN_QTY", TRUE_MAIN_QTY);
        map.addProperty("CUSTOMER_CODE", CUSTOMER_CODE);
        map.addProperty("MAIN_MAT_TYPE", getText(fengzuang));
        map.addProperty("PRODUCT_MODEL", getText(xinghao));
        map.addProperty("STEP_DESC", STEP_DESC);
        map.addProperty("STEP_NAME", STEP_NAME);
        map.addProperty("USER_NAME", USER_NAME);
        map.addProperty("NAME", NAME);
        map.addProperty("POIN_LOCATION",tvPointType.getText().toString());
        map.addProperty("LOCATION", shebei.getText().toString().trim());
        map.addProperty("IS_PASS", IS_PASS);
        map.addProperty("BEI_ZU", BEI_ZU);
        map.addProperty("SEND_TIME", SEND_TIME);
        NetUtils.requestPostNet("IF.PDA_POINT_NUMBER", "Common", BreLanjianPointNumMainActivity.this, map, callBack);
    }
}